//
//  BIDEmailInboxViewController.m
//  shangwuting
//
//  Created by mal on 13-12-25.
//  Copyright (c) 2013年 mal. All rights reserved.
//

#import "BIDEmailInboxViewController.h"
#import "BIDReceivedEmailCell.h"
#import "BIDDetailEmailFromInboxViewController.h"
#import "BIDEmailDetailForReceiveViewController.h"

//收件箱列表
static NSString *receivedEmailListURL = @"emailReplyAndroid!inboxListJson.action";
//邮件删除
static NSString *deleteEmailURL = @"emailReplyAndroid!dropInbox.action";

@interface BIDEmailInboxViewController ()

@end

@implementation BIDEmailInboxViewController
@synthesize bRegister;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"收件箱";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    bRegister = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.dataSourceArr removeAllObjects];
    self.curPage = 0;
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], receivedEmailListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"userId=%@&page=%ld&rows=%ld", [AppDelegate getUserId], self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count<self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
            [self.myTableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//删除列表中的一个邮件
- (void)deleteEmailAtIndexPath:(NSIndexPath*)indexPath
{
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteEmailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@", dic[@"id"]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [self.myTableView beginUpdates];
                [self.dataSourceArr removeObjectAtIndex:indexPath.row];
                [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                [self.myTableView endUpdates];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"删除失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark UITableViewControllerDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    static NSString *identifier = @"identifier";
    if(!bRegister)
    {
        bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDReceivedEmailCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDReceivedEmailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dictionary = self.dataSourceArr[row];
    NSNumber *value = [dictionary objectForKey:@"isRead"];
    if([value intValue] == 2)
    {
        //已读
        [cell setReadFlag:YES];
    }
    else
    {
        //未读
        [cell setReadFlag:NO];
    }
        
    cell.titleLabel.text = dictionary[@"title"];
    cell.dateLabel.text = [dictionary objectForKey:@"receiveTime"];
    cell.nameLabel.text = [[NSString alloc] initWithFormat:@"发送人:%@", [dictionary objectForKey:@"trueName"]];
    [cell setBackgroundColor:[UIColor clearColor]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    //BIDDetailEmailFromInboxViewController *vc = [[BIDDetailEmailFromInboxViewController alloc] initWithNibName:@"BIDDetailEmailFromInboxViewController" bundle:nil];
    BIDEmailDetailForReceiveViewController *vc = [[BIDEmailDetailForReceiveViewController alloc] initWithNibName:@"BIDEmailDetailForReceiveViewController" bundle:nil];
    vc.emailID = [dic objectForKey:@"emailId"];
    vc.deleteID = [dic objectForKey:@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 79.f;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self deleteEmailAtIndexPath:indexPath];
    }
}

@end
